package io.renren.modules.newBackground.shanghaiCmcc.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.SnowflakeIdUtil;
import io.renren.modules.newBackground.shanghaiCmcc.dto.OrderPubInfo;
import io.renren.modules.newBackground.shanghaiCmcc.dto.ShangHaiBackgroundUnsubscribeReq;
import io.renren.modules.newBackground.shanghaiCmcc.dto.ShangHaiCmccUnsubscribeReq;
import io.renren.modules.newBackground.shanghaiCmcc.v2.client.OpenapiHttpCilent;
import io.renren.modules.wap.entity.ExternalOrdersEntity;
import io.renren.modules.wap.service.ExternalOrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DataSource("wap")
public class ShangHaiCmccBackgroundServiceImpl implements ShangHaiCmccBackgroundService {
    private static final Logger logger = LoggerFactory.getLogger(ShangHaiCmccBackgroundServiceImpl.class);

    private static final String OFFER_ID = "CRM6410";

    private static final String APPID = "shanghai-cmcc";

    private static final String APP_CODE = "A0029646";

    private static final String APP_KEY = "Js5JpClGGpQTUtAxvwTsW0+D7yBJkix1";

    @Autowired
    private ExternalOrdersService externalOrdersService;


    public void queryUnsubscribe(ShangHaiBackgroundUnsubscribeReq backgroundUnsubscribeReq) {


        OpenapiHttpCilent httpsClient = new OpenapiHttpCilent(APP_CODE, APP_KEY);

        //组装通用字段
        OrderPubInfo orderPubInfo = new OrderPubInfo();

        List<ExternalOrdersEntity> orderList = null;
        if (StrUtil.isNotBlank(backgroundUnsubscribeReq.getMobile())) {
            // 通过手机号查询订单信息
            orderList = externalOrdersService.getByPhoneAndAppId(backgroundUnsubscribeReq.getMobile(), APPID);
        } else {
            //查询日期内订单
            orderList = externalOrdersService.getByDate(backgroundUnsubscribeReq.getStartTime(), backgroundUnsubscribeReq.getEndTime());
        }
        //遍历查询退订信息
        for (ExternalOrdersEntity order : orderList) {
            ShangHaiCmccUnsubscribeReq shangHaiCmccUnsubscribeReq = new ShangHaiCmccUnsubscribeReq();
            ShangHaiCmccUnsubscribeReq.BusiParams busiParams = new ShangHaiCmccUnsubscribeReq.BusiParams();
            shangHaiCmccUnsubscribeReq.setPubInfo(orderPubInfo);

            ShangHaiCmccUnsubscribeReq.Request request = new ShangHaiCmccUnsubscribeReq.Request();
            shangHaiCmccUnsubscribeReq.setRequest(request);

            //yyyyMMddHHmm
            busiParams.setOrderTime(DateUtils.format(order.getCreateTime(), DateUtils.DATE_TIME_PATTERN_3));
            busiParams.setBillId(order.getPhone());
            String goodsIdStr = order.getGoodsId();
            String[] goodsIds = goodsIdStr.split(",");
            for(String goodId : goodsIds){
                busiParams.setOfferId(goodId);
                request.setBusiParams(busiParams);
                String businessId = SnowflakeIdUtil.getStrSnowId(3, 4);
                orderPubInfo.setTransactionId(businessId);
                String jsonStr = JSON.toJSONString(shangHaiCmccUnsubscribeReq);
                logger.info("上海移动退订查询入参:{}", jsonStr);
                try {
                    String respStr = httpsClient.call(OFFER_ID, businessId, jsonStr);
                    logger.info("上海移动退订查询出参:{}", respStr);
                    System.out.println(1);

                } catch (Exception e) {
                    logger.error("上海移动退订查询异常", e);
                }
            }


        }

    }
}
